﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data;


/******
 * ** 1、F_OUTPUT里的suoding是记录环保人员审核后锁定情况的，当suoding=1时，排放口及排放口相关的申报数据都不能修改及删除了
 * 
 * 
 * 
 * 
 * 
 **/

public partial class _2_2_1 : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        using (DataClassesDataContext mydata = new DataClassesDataContext())
        {
            if (!IsPostBack)
            {
                LabelTiShi.Visible = false;

                // 读取表a01101的经纬度到HiddenFieldJD和HiddenFieldWD
                HiddenFieldJD.Value = "000度00分00秒";
                HiddenFieldWD.Value = "00度00分00秒";
                TextBoxJD.Text = HiddenFieldJD.Value.ToString();
                TextBoxWD.Text = HiddenFieldWD.Value.ToString();
                var query = (from q in mydata.a01101 where q.qiyeid == Convert.ToInt32(Session["qiyeid"]) select q).FirstOrDefault();
                if (query != null)
                {
                    if (!(query.XAIS == null || query.XAIS == ""))
                    {
                        HiddenFieldJD.Value = query.XAIS.ToString().Substring(0, 3) + "度" + query.XAIS.ToString().Substring(3, 2) + "分" + query.XAIS.ToString().Substring(5, 2) + "秒";
                    }
                    if (!(query.YAIS == null || query.YAIS == ""))
                    {
                        HiddenFieldWD.Value = query.YAIS.ToString().Substring(0, 2) + "度" + query.YAIS.ToString().Substring(2, 2) + "分" + query.YAIS.ToString().Substring(4, 2) + "秒";
                    }
                }

                var query2 = (from a in mydata.a01201_1
                              join f in mydata.F_OUTPUT
                                  on new { a.qiyeid, a.SERIAL } equals new { f.qiyeid, f.SERIAL }
                              where a.DATA_MONTH == Convert.ToString(Session["ShenBaoNianDu"]) && a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                              where f.POLLUTE_TYPE.ToString() == "1" && a.SERIAL < 1000
                              select a).FirstOrDefault();  //可能有几个排放口的数据，默认显示本申报年度有数据的第一个排放口
                if (query2 != null)
                {
                    DuShuJu(query2.SERIAL.ToString());
                    DropDownListPFK.SelectedValue = query2.SERIAL.ToString();
                    HiddenFieldMain.Value = "edit";
                }
                else
                {
                    ClearInput();
                }
            }
        }
    }

    public string DisplayStr()
    {

        YingYongHanShu yyhs = new YingYongHanShu();

        return yyhs.DisplayStr2("101201");
    }


    //设置排放口的相关信息是否只读。当该排放口是排污征收管理系统里存在的，则不允许修改
    public void SetReadOnly(bool blrd)
    {
        TextBoxPFKMC.Enabled = !blrd;
        TextBoxPFKBH.Enabled = !blrd;
        TextBoxPFKWZ.Enabled = !blrd;
        DropDownListGNQLB.Enabled = !blrd;
        DropDownListWSPFGL.Enabled = !blrd;
        DropDownListPFQX.Enabled = !blrd;
        DropDownListSTMC.Enabled = !blrd;
        TextBoxZDJKY.Enabled = !blrd;
    }


    //刷新排放口下拉框显示
    public void ShuaXinXianShi(string SERIAL)
    {

        DropDownListPFK.Items.Clear();
        DropDownListPFK.Items.Add("新增排放口");
        DropDownListPFK.DataBind();
        if (SERIAL != "-1")
        {
            DropDownListPFK.SelectedIndex = DropDownListPFK.Items.IndexOf(DropDownListPFK.Items.FindByValue(SERIAL));
        }
        else//删除排放口后的显示
        {
            DropDownListPFK.SelectedIndex = 0;
        }
    }


    //清空输入框
    public void ClearInput()
    {
        HiddenField[] hb = new HiddenField[] { this.HiddenField1, this.HiddenField2, this.HiddenField3, this.HiddenField4, this.HiddenField5, this.HiddenField6, this.HiddenField7, this.HiddenField8, this.HiddenField9, this.HiddenField10, this.HiddenField11, this.HiddenField12, this.HiddenField13, this.HiddenField14, this.HiddenField15, this.HiddenField16 };
        TextBox[] tbxHJ = new TextBox[] { this.TextBoxHJ1, this.TextBoxHJ2, this.TextBoxHJ3, this.TextBoxHJ4, this.TextBoxHJ5, this.TextBoxHJ6, this.TextBoxHJ7, this.TextBoxHJ8, this.TextBoxHJ9, this.TextBoxHJ10, this.TextBoxHJ11, this.TextBoxHJ12, this.TextBoxHJ13, this.TextBoxHJ14, this.TextBoxHJ15, this.TextBoxHJ16 };
        TextBox[] tbxDBL = new TextBox[] { this.TextBoxDBL1, this.TextBoxDBL2, this.TextBoxDBL3, this.TextBoxDBL4, this.TextBoxDBL5, this.TextBoxDBL6, this.TextBoxDBL7, this.TextBoxDBL8, this.TextBoxDBL9, this.TextBoxDBL10, this.TextBoxDBL11, this.TextBoxDBL12, this.TextBoxDBL13, this.TextBoxDBL14, this.TextBoxDBL15, this.TextBoxDBL16 };
        TextBox[] tbxCBL = new TextBox[] { this.TextBoxCBL1, this.TextBoxCBL2, this.TextBoxCBL3, this.TextBoxCBL4, this.TextBoxCBL5, this.TextBoxCBL6, this.TextBoxCBL7, this.TextBoxCBL8, this.TextBoxCBL9, this.TextBoxCBL10, this.TextBoxCBL11, this.TextBoxCBL12, this.TextBoxCBL13, this.TextBoxCBL14, this.TextBoxCBL15, this.TextBoxCBL16 };
        DropDownList[] dpl = new DropDownList[] { this.DropDownList12, this.DropDownList13, this.DropDownList14, this.DropDownList15, this.DropDownList16 };

        HiddenFieldMain.Value = "insert";
        HiddenFieldOUTPUT_EDIT.Value = "insert";
        HiddenFieldA01201_1.Value = "insert";

        SetReadOnly(false);//设置排放口相关信息的输入框为可改写
        TextBoxPFKMC.Text = "";
        TextBoxPFKBH.Text = "";
        TextBoxPFKWZ.Text = "";
        DropDownListGNQLB.SelectedIndex = -1;
        DropDownListWSPFGL.SelectedIndex = -1;
        TextBoxPFTS.Text = "";
        TextBoxPFSJ.Text = "";
        DropDownListPFQX.SelectedIndex = -1;
        DropDownListSTMC.SelectedIndex = -1;
        TextBoxWSPFL.Text = "";
        TextBoxQZDBPFL.Text = "";
        TextBoxQZCBPFL.Text = "";
        for (int i = 0; i < 12; i++)
        {
            CheckBoxList1.Items[i].Selected = false;
        }
        TextBoxJSXMMC.Text = "";
        TextBoxJSRQ.Text = "";
        TextBoxZDJKY.Text = "";
        for (int i = 0; i < 16; i++)
        {
            hb[i].Value = "insert";
            tbxHJ[i].Text = "";
            tbxDBL[i].Text = "";
            tbxCBL[i].Text = "";
            if (i >= 11)
            {
                dpl[i - 11].SelectedIndex = -1;
            }
        }
    }


    //读一个排放口的资料数据和对应的本年度的申报数据,调用前要判断SERIAL不能为空，确保是有数据的排口
    public void DuShuJu(string SERIAL)
    {
        using (DataClassesDataContext mydata = new DataClassesDataContext())
        {
            ClearInput();

            var query = (from f in mydata.F_OUTPUT
                         where f.qiyeid == Convert.ToInt32(Session["qiyeid"]) && f.SERIAL == Convert.ToInt32(SERIAL)
                         select new
                         {
                             pfkmc = f.OUT_NAME,
                             pfkbh = f.OUTPUT_CODE,
                             pfkwz = f.OUTPUT_POSITION,
                             gnqlb = f.FUNCTION_REGION == null ? -1 : Convert.ToInt32(f.FUNCTION_REGION),
                             pfqx = f.OUT_DIRECTION == null ? "" : Convert.ToString(f.OUT_DIRECTION),
                             stmc = f.VALLEY_CODE == null ? "" : Convert.ToString(f.VALLEY_CODE),
                             wspfgl = f.OUT_RULE == null ? "" : Convert.ToString(f.OUT_RULE),
                             suoding = f.suoding == null ? false : Convert.ToBoolean(f.suoding),
                             system = f.system == null ? false : Convert.ToBoolean(f.system)
                         }).FirstOrDefault();
            if (query != null)
            {
                //*************排放口信息*********
                TextBoxPFKMC.Text = query.pfkmc;
                TextBoxPFKBH.Text = query.pfkbh;
                TextBoxPFKWZ.Text = query.pfkwz;
                TextBoxJD.Text = Convert.ToString(HiddenFieldJD.Value);
                TextBoxWD.Text = Convert.ToString(HiddenFieldWD.Value);
                DropDownListGNQLB.SelectedIndex = query.gnqlb;
                if (query.pfqx.Trim() == "")
                {
                    DropDownListPFQX.SelectedIndex = -1;
                }
                else
                {
                    DropDownListPFQX.SelectedValue = query.pfqx;
                }
                if (query.stmc.Trim() == "")
                {
                    DropDownListSTMC.SelectedIndex = -1;
                }
                else
                {
                    if (query.stmc.Trim().Substring(0, 2) == "16")//流域代码16开头的是“粤桂琼沿海诸河流域”，除了茂名外，其他地区要改
                    {
                        DropDownListSTMC.SelectedValue = query.stmc;
                    }
                    else
                    {
                        DropDownListSTMC.SelectedIndex = -1;
                    }

                }
                if (query.wspfgl.Trim() == "")
                {
                    DropDownListWSPFGL.SelectedIndex = -1;
                }
                else
                {
                    DropDownListWSPFGL.SelectedValue = query.wspfgl;
                }

                //判断排放口信息是否可以修改
                SetReadOnly(query.suoding || query.system);//如果该排放口信息已被审核后锁定或是排污征收系统数据库导出的数据则不能修改
                if (query.suoding || query.system) //置排放口属性信息不可修改标志
                {
                    HiddenFieldOUTPUT_EDIT.Value = "insert";
                }
                else
                {
                    HiddenFieldOUTPUT_EDIT.Value = "edit";
                }

                //************排放口除了污染物外申报数据*********
                var q = (from a in mydata.a01201_1
                         where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                         where a.DATA_MONTH == Convert.ToString(Session["ShenBaoNianDu"])
                         where a.SERIAL == Convert.ToInt32(SERIAL)
                         select new
                         {
                             pfts = a.OUT_DAYS == null ? "" : string.Format("{0:0}", a.OUT_DAYS),
                             pfsj = a.HOURS_DAY == null ? "" : string.Format("{0:0}", a.HOURS_DAY),
                             wspfl = a.SUM_COUNT == null ? "" : string.Format("{0:#.####}", a.SUM_COUNT),
                             qzdbpfl = a.REACH_CNT == null ? "" : string.Format("{0:#.####}", a.REACH_CNT),
                             qzcbpfl = a.OVER_CNT == null ? "" : string.Format("{0:#.####}", a.OVER_CNT),
                             pfyf = a.OUT_MONTH == null ? "" : Convert.ToString(a.OUT_MONTH)
                         }).FirstOrDefault();
                if (q != null)
                {
                    HiddenFieldA01201_1.Value = "edit"; //置该排放口申报数据可修改标记

                    TextBoxPFTS.Text = q.pfts;
                    TextBoxPFSJ.Text = q.pfsj;
                    TextBoxWSPFL.Text = q.wspfl;
                    TextBoxQZDBPFL.Text = q.qzdbpfl;
                    TextBoxQZCBPFL.Text = q.qzcbpfl;

                    //排放月份
                    for (int i = 0; i < 12; i++)
                    {
                        CheckBoxList1.Items[i].Selected = false;
                    }
                    string[] fh = new string[] { "," };
                    string[] yf;
                    string s = q.pfyf;
                    if (q.pfyf != "")
                    {
                        yf = s.Split(fh, StringSplitOptions.None);

                        foreach (var cs in yf)
                        {
                            CheckBoxList1.Items[Convert.ToInt32(cs) - 1].Selected = true;
                        }
                    }
                }


                //**********读取污染物数据*********** 
                HiddenField[] hb = new HiddenField[] { this.HiddenField1, this.HiddenField2, this.HiddenField3, this.HiddenField4, this.HiddenField5, this.HiddenField6, this.HiddenField7, this.HiddenField8, this.HiddenField9, this.HiddenField10, this.HiddenField11, this.HiddenField12, this.HiddenField13, this.HiddenField14, this.HiddenField15, this.HiddenField16 };
                TextBox[] tbxHJ = new TextBox[] { this.TextBoxHJ1, this.TextBoxHJ2, this.TextBoxHJ3, this.TextBoxHJ4, this.TextBoxHJ5, this.TextBoxHJ6, this.TextBoxHJ7, this.TextBoxHJ8, this.TextBoxHJ9, this.TextBoxHJ10, this.TextBoxHJ11, this.TextBoxHJ12, this.TextBoxHJ13, this.TextBoxHJ14, this.TextBoxHJ15, this.TextBoxHJ16 };
                TextBox[] tbxDBL = new TextBox[] { this.TextBoxDBL1, this.TextBoxDBL2, this.TextBoxDBL3, this.TextBoxDBL4, this.TextBoxDBL5, this.TextBoxDBL6, this.TextBoxDBL7, this.TextBoxDBL8, this.TextBoxDBL9, this.TextBoxDBL10, this.TextBoxDBL11, this.TextBoxDBL12, this.TextBoxDBL13, this.TextBoxDBL14, this.TextBoxDBL15, this.TextBoxDBL16 };
                TextBox[] tbxCBL = new TextBox[] { this.TextBoxCBL1, this.TextBoxCBL2, this.TextBoxCBL3, this.TextBoxCBL4, this.TextBoxCBL5, this.TextBoxCBL6, this.TextBoxCBL7, this.TextBoxCBL8, this.TextBoxCBL9, this.TextBoxCBL10, this.TextBoxCBL11, this.TextBoxCBL12, this.TextBoxCBL13, this.TextBoxCBL14, this.TextBoxCBL15, this.TextBoxCBL16 };
                DropDownList[] dpl = new DropDownList[] { this.DropDownList12, this.DropDownList13, this.DropDownList14, this.DropDownList15, this.DropDownList16 };

                for (int i = 0; i < 16; i++)//转换另外的排放口的初始化数据
                {
                    hb[i].Value = "insert";
                }

                var record = from a in mydata.a01201_2
                             where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                             where a.DATA_MONTH == Convert.ToString(Session["ShenBaoNianDu"])
                             where a.SERIAL == Convert.ToInt32(SERIAL)
                             select a;
                if (record != null)
                {
                    int xh;
                    foreach (var r in record)
                    {
                        xh = Convert.ToInt32(r.SRKXH) - 1;
                        hb[xh].Value = "edit";
                        if (xh >= 11)
                        {
                            dpl[xh - 11].SelectedValue = r.CODE == null ? "" : r.CODE;
                        }
                        tbxHJ[xh].Text = r.POLLUTE_OUTCOUNT == null ? "" : string.Format("{0:#.####}", r.POLLUTE_OUTCOUNT);
                        tbxDBL[xh].Text = r.REACH_COUNT == null ? "" : string.Format("{0:#.####}", r.REACH_COUNT);
                        tbxCBL[xh].Text = r.OVER_COUNT == null ? "" : string.Format("{0:#.####}", r.OVER_COUNT);
                    }
                }
            }
        }
    }


    protected void ButtonTiJiao_Click(object sender, EventArgs e)
    {
        HiddenField[] hb = new HiddenField[] { this.HiddenField1, this.HiddenField2, this.HiddenField3, this.HiddenField4, this.HiddenField5, this.HiddenField6, this.HiddenField7, this.HiddenField8, this.HiddenField9, this.HiddenField10, this.HiddenField11, this.HiddenField12, this.HiddenField13, this.HiddenField14, this.HiddenField15, this.HiddenField16 };

        if (Page.IsValid) //当所有校验控件都正确是才提交
        {
            using (DataClassesDataContext mydata = new DataClassesDataContext())
            {
                //得到排放口的SERIAL
                string SERIAL = "0";
                if (DropDownListPFK.SelectedIndex != 0)//第一个item要永远保证是“新增排放口”
                {
                    SERIAL = Convert.ToString(DropDownListPFK.SelectedValue);//排放口变量赋值 
                }
                else //新增排放口要生成新的SERIAL
                {
                    int? max = (from f in mydata.F_OUTPUT
                                where f.qiyeid == Convert.ToInt32(Session["qiyeid"])
                                where f.SERIAL < 1000
                                select (int?)f.SERIAL).Max();
                    if (!max.HasValue)
                        SERIAL = "1";
                    else
                        SERIAL = Convert.ToString(max + 1);
                }

                //**********逻辑判断***********
                //应加入判断 1、任何时候，排放口名称和排放口编号不能为空，同时不能跟已有的排放口名称、编号重名;
                //                      [3.排放口位置]、[6.功能区类别]、[11.排放去向]、[12.水体名称]不能为空;
                //                       行业类别由基本信息表导入
                //          2、

                string strCuoWu = "提交内容有错误：";//记录错误内容
                int CuoWuXuHao = 0;//错误序号
                bool TuiChu = false; //记录逻辑判断过程中是否有错误需要中断退出提交过程
                if (TextBoxPFKMC.Text.Trim() == "")
                {
                    CuoWuXuHao++;
                    strCuoWu = strCuoWu + Convert.ToString(CuoWuXuHao) + "、" + "排放口名称不能为空。";
                    TuiChu = true;
                }
                else
                {
                    bool cz1 = (from f in mydata.F_OUTPUT
                                where f.qiyeid == Convert.ToInt32(Session["qiyeid"])
                                where f.SERIAL != Convert.ToInt32(SERIAL)
                                where f.OUT_NAME == TextBoxPFKMC.Text.Trim()
                                select f).Any();
                    if (cz1 == true)
                    {
                        CuoWuXuHao++;
                        strCuoWu = strCuoWu + Convert.ToString(CuoWuXuHao) + "、" + "已有排放口'" + TextBoxPFKMC.Text.Trim() + "',不能重复。<br/>";
                        TuiChu = true;
                    }
                }

                if (TextBoxPFKBH.Text.Trim() == "")
                {
                    CuoWuXuHao++;
                    strCuoWu = strCuoWu + Convert.ToString(CuoWuXuHao) + "、" + "排放口编号不能为空。";
                    TuiChu = true;
                }
                else
                {
                    bool cz2 = (from f in mydata.F_OUTPUT
                                where f.qiyeid == Convert.ToInt32(Session["qiyeid"])
                                where f.SERIAL != Convert.ToInt32(SERIAL)
                                where f.OUTPUT_CODE == TextBoxPFKBH.Text.Trim()
                                select f).Any();

                    if (cz2 == true)
                    {
                        CuoWuXuHao++;
                        strCuoWu = strCuoWu + Convert.ToString(CuoWuXuHao) + "、" + "已有排放口编号'" + TextBoxPFKBH.Text.Trim() + "',不能重复。<br/>";
                        TuiChu = true;
                    }
                }

                if (TextBoxPFKWZ.Text.Trim() == "")
                {
                    strCuoWu = strCuoWu + Convert.ToString(CuoWuXuHao) + "、" + " 排放口位置不能为空";
                }

                if (TuiChu)
                {
                    Response.Write("<script>alert('提交内容有错误，请检查!')</script>");
                    TextBoxCuoWuXinXi.Text = strCuoWu;
                    return;//中断退出提交过程
                }

                LabelTiShi.Text = "正在提交中，请不要关闭或离开...";
                LabelTiShi.Visible = true;
                TextBoxCuoWuXinXi.Text = "";


                //**************新增或修改排放口信息
                if (HiddenFieldMain.Value == "edit")//在DropDownListPFK里选择的不是“新增排放口”
                {
                    if (HiddenFieldOUTPUT_EDIT.Value == "edit")//排放口不是是排污系统数据库导出或不是已通过审核锁定的，可修改
                    {
                        string jd = Convert.ToString(HiddenFieldJD.Value);
                        string wd = Convert.ToString(HiddenFieldWD.Value);

                        var rd = (from f in mydata.F_OUTPUT
                                  where f.qiyeid == Convert.ToInt32(Session["qiyeid"])
                                  where f.SERIAL == Convert.ToInt32(SERIAL)
                                  select f).FirstOrDefault();
                        rd.OUT_NAME = TextBoxPFKMC.Text.Trim();
                        rd.OUTPUT_CODE = TextBoxPFKBH.Text.Trim();
                        rd.OUTPUT_POSITION = TextBoxPFKWZ.Text.Trim();
                        rd.XAIS = Convert.ToDouble(jd.Substring(0, 3) + jd.Substring(4, 2) + jd.Substring(7, 2));
                        rd.YAIS = Convert.ToDouble(wd.Substring(0, 2) + wd.Substring(3, 2) + wd.Substring(6, 2));
                        rd.FUNCTION_REGION = Convert.ToInt32(DropDownListGNQLB.SelectedValue);
                        rd.OUT_RULE = Convert.ToString(DropDownListWSPFGL.SelectedValue);
                        rd.OUT_DIRECTION = Convert.ToChar(DropDownListPFQX.SelectedValue);
                        rd.VALLEY_CODE = Convert.ToString(DropDownListSTMC.SelectedValue);
                        rd.FACILITY_NAME = TextBoxZDJKY.Text.Trim();
                        mydata.SubmitChanges();
                    }
                }
                else//新增排放口
                {
                    F_OUTPUT p = new F_OUTPUT();
                    p.OUT_NAME = TextBoxPFKMC.Text.Trim();
                    p.OUTPUT_CODE = TextBoxPFKBH.Text.Trim();
                    p.OUTPUT_POSITION = TextBoxPFKWZ.Text.Trim();
                    p.XAIS = Convert.ToDouble(HiddenFieldJD.Value.ToString().Substring(0, 3) + HiddenFieldJD.Value.ToString().Substring(4, 2) + HiddenFieldJD.Value.ToString().Substring(7, 2));
                    p.YAIS = Convert.ToDouble(HiddenFieldWD.Value.ToString().Substring(0, 2) + HiddenFieldWD.Value.ToString().Substring(3, 2) + HiddenFieldWD.Value.ToString().Substring(6, 2));
                    p.FUNCTION_REGION = Convert.ToInt32(DropDownListGNQLB.SelectedValue);
                    p.OUT_RULE = Convert.ToString(DropDownListWSPFGL.SelectedValue);
                    p.OUT_DIRECTION = Convert.ToChar(DropDownListPFQX.SelectedValue);
                    p.VALLEY_CODE = Convert.ToString(DropDownListSTMC.SelectedValue);
                    p.FACILITY_NAME = TextBoxZDJKY.Text.Trim();
                    p.qiyeid = Convert.ToInt32(Session["qiyeid"]);
                    p.SERIAL = Convert.ToInt32(SERIAL);
                    p.POLLUTE_TYPE = Convert.ToChar("1");
                    p.LOGOUT_FLAG = 0;
                    mydata.F_OUTPUT.InsertOnSubmit(p);
                    mydata.SubmitChanges();

                    HiddenFieldOUTPUT_EDIT.Value = "edit";
                    HiddenFieldMain.Value = "edit";
                    HiddenFieldA01201_1.Value = "insert";

                    for (int i = 0; i < 16; i++)//转换另外的排放口的初始化数据
                    {
                        hb[i].Value = "insert";
                    }

                    ShuaXinXianShi(SERIAL);
                }


                //*******************保存排放口除了污染物外本年度申报数据
                ZhuanHuan zh = new ZhuanHuan();//调用转换类
                string sss = "";
                for (int i = 0; i < 12; i++)//排放月份
                {
                    if (CheckBoxList1.Items[i].Selected == true)
                    {
                        sss = sss + Convert.ToString(i + 1) + ",";
                    }
                }
                if (sss != "")
                {
                    sss = sss.Substring(0, sss.Length - 1);//去掉最后的","  
                }

                if (HiddenFieldA01201_1.Value == "edit")
                {
                    var rd3 = (from a in mydata.a01201_1
                               where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                               where a.DATA_MONTH == Convert.ToString(Session["ShenBaoNianDu"])
                               where a.SERIAL == Convert.ToInt32(SERIAL)
                               select a).First();
                    rd3.OUT_DAYS = zh.StrToDouble(TextBoxPFTS.Text.Trim());
                    rd3.HOURS_DAY = zh.StrToDouble(TextBoxPFSJ.Text.Trim());
                    rd3.SUM_COUNT = zh.StrToDouble(TextBoxQZDBPFL.Text.Trim()) + zh.StrToDouble(TextBoxQZCBPFL.Text.Trim());
                    rd3.REACH_CNT = zh.StrToDouble(TextBoxQZDBPFL.Text.Trim());
                    rd3.OVER_CNT = zh.StrToDouble(TextBoxQZCBPFL.Text.Trim());
                    rd3.OUT_MONTH = sss;
                    rd3.PROJECT_NAME = TextBoxJSXMMC.Text.Trim();
                    mydata.SubmitChanges();
                }
                else
                {
                    a01201_1 p = new a01201_1();
                    p.OUT_DAYS = zh.StrToDouble(TextBoxPFTS.Text.Trim());
                    p.HOURS_DAY = zh.StrToDouble(TextBoxPFSJ.Text.Trim());
                    p.SUM_COUNT = zh.StrToDouble(TextBoxQZDBPFL.Text.Trim()) + zh.StrToDouble(TextBoxQZCBPFL.Text.Trim());
                    p.REACH_CNT = zh.StrToDouble(TextBoxQZDBPFL.Text.Trim());
                    p.OVER_CNT = zh.StrToDouble(TextBoxQZCBPFL.Text.Trim());
                    p.OUT_MONTH = sss;
                    p.PROJECT_NAME = TextBoxJSXMMC.Text.Trim();
                    p.qiyeid = Convert.ToInt32(Session["qiyeid"]);
                    p.DATA_MONTH = Convert.ToString(Session["ShenBaoNianDu"]);
                    p.SERIAL = Convert.ToInt32(SERIAL);
                    mydata.a01201_1.InsertOnSubmit(p);
                    mydata.SubmitChanges();

                    HiddenFieldA01201_1.Value = "edit";

                }

                //************update 污染物********
                //********申报表里污染物（1）汞 到 （11）悬浮物*********
                //
                //污染物代码，按申报表里污染物名称前面的序号排列,记录(1)汞到(11)悬浮物
                //'020'汞  '022'镉   '024'六价铬  '027'铅 '026'砷  '110'挥发酚  '070' 氰化物 '011'化学需氧量  '080'石油类  '060' 氨氮 '003'悬浮物  
                string[] wrwdm = new string[] { "020", "022", "024", "027", "026", "110", "070", "011", "080", "060", "003" };

                //HiddenField[] hb = new HiddenField[] { this.HiddenField1, this.HiddenField2, this.HiddenField3, this.HiddenField4, this.HiddenField5, this.HiddenField6, this.HiddenField7, this.HiddenField8, this.HiddenField9, this.HiddenField10, this.HiddenField11, this.HiddenField12, this.HiddenField13, this.HiddenField14, this.HiddenField15, this.HiddenField16 };
                TextBox[] tbxHJ = new TextBox[] { this.TextBoxHJ1, this.TextBoxHJ2, this.TextBoxHJ3, this.TextBoxHJ4, this.TextBoxHJ5, this.TextBoxHJ6, this.TextBoxHJ7, this.TextBoxHJ8, this.TextBoxHJ9, this.TextBoxHJ10, this.TextBoxHJ11, this.TextBoxHJ12, this.TextBoxHJ13, this.TextBoxHJ14, this.TextBoxHJ15, this.TextBoxHJ16 };
                TextBox[] tbxDBL = new TextBox[] { this.TextBoxDBL1, this.TextBoxDBL2, this.TextBoxDBL3, this.TextBoxDBL4, this.TextBoxDBL5, this.TextBoxDBL6, this.TextBoxDBL7, this.TextBoxDBL8, this.TextBoxDBL9, this.TextBoxDBL10, this.TextBoxDBL11, this.TextBoxDBL12, this.TextBoxDBL13, this.TextBoxDBL14, this.TextBoxDBL15, this.TextBoxDBL16 };
                TextBox[] tbxCBL = new TextBox[] { this.TextBoxCBL1, this.TextBoxCBL2, this.TextBoxCBL3, this.TextBoxCBL4, this.TextBoxCBL5, this.TextBoxCBL6, this.TextBoxCBL7, this.TextBoxCBL8, this.TextBoxCBL9, this.TextBoxCBL10, this.TextBoxCBL11, this.TextBoxCBL12, this.TextBoxCBL13, this.TextBoxCBL14, this.TextBoxCBL15, this.TextBoxCBL16 };

                for (int i = 0; i <= 10; i++)
                {
                    if (hb[i].Value == "edit")
                    {
                        var rd4 = (from a in mydata.a01201_2
                                   where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                                   where a.DATA_MONTH == Convert.ToString(Session["ShenBaoNianDu"])
                                   where a.SERIAL == Convert.ToInt32(SERIAL)
                                   where a.SRKXH == Convert.ToString(i + 1)
                                   select a).First();

                        rd4.POLLUTE_OUTCOUNT = zh.StrToDouble(tbxDBL[i].Text.Trim()) + zh.StrToDouble(tbxCBL[i].Text.Trim());
                        rd4.REACH_COUNT = zh.StrToDouble(tbxDBL[i].Text.Trim());
                        rd4.OVER_COUNT = zh.StrToDouble(tbxCBL[i].Text.Trim());
                        mydata.SubmitChanges();

                        tbxHJ[i].Text = Convert.ToString(zh.StrToDouble(tbxDBL[i].Text.Trim()) + zh.StrToDouble(tbxCBL[i].Text.Trim()));//解决提交页面刷新后变动回旧值得问题
                    }
                    else//新增
                    {
                        if ((tbxDBL[i].Text.Trim() != "") || (tbxCBL[i].Text.Trim() != ""))
                        {
                            a01201_2 p = new a01201_2();

                            p.POLLUTE_OUTCOUNT = zh.StrToDouble(tbxDBL[i].Text.Trim()) + zh.StrToDouble(tbxCBL[i].Text.Trim());
                            p.REACH_COUNT = zh.StrToDouble(tbxDBL[i].Text.Trim());
                            p.OVER_COUNT = zh.StrToDouble(tbxCBL[i].Text.Trim());
                            p.SRKXH = Convert.ToString(i + 1);
                            p.CODE = wrwdm[i];
                            p.DATA_MONTH = Convert.ToString(Session["ShenBaoNianDu"]);
                            p.qiyeid = Convert.ToInt32(Session["qiyeid"]);
                            p.SERIAL = Convert.ToInt32(SERIAL);
                            mydata.a01201_2.InsertOnSubmit(p);
                            mydata.SubmitChanges();

                            hb[i].Value = "edit";
                            tbxHJ[i].Text = Convert.ToString(zh.StrToDouble(tbxDBL[i].Text.Trim()) + zh.StrToDouble(tbxCBL[i].Text.Trim()));//解决提交页面刷新后变动回旧值得问题
                        }
                    }
                }

                //*******污染物(11) 到 (16)*********用户自己选择污染物呢名称的
                DropDownList[] dpl = new DropDownList[] { this.DropDownList12, this.DropDownList13, this.DropDownList14, this.DropDownList15, this.DropDownList16 };
                for (int i = 11; i <= 15; i++)
                {
                    if (hb[i].Value == "edit")
                    {
                        var rd5 = (from a in mydata.a01201_2
                                   where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                                   where a.SRKXH == Convert.ToString(i + 1)
                                   where a.DATA_MONTH == Convert.ToString(Session["ShenBaoNianDu"])
                                   where a.SERIAL == Convert.ToInt32(SERIAL)
                                   select a).First();

                        rd5.POLLUTE_OUTCOUNT = zh.StrToDouble(tbxDBL[i].Text.Trim()) + zh.StrToDouble(tbxCBL[i].Text.Trim());
                        rd5.REACH_COUNT = zh.StrToDouble(tbxDBL[i].Text.Trim());
                        rd5.OVER_COUNT = zh.StrToDouble(tbxCBL[i].Text.Trim());
                        rd5.CODE = dpl[i - 11].SelectedValue;
                        mydata.SubmitChanges();

                        tbxHJ[i].Text = Convert.ToString(zh.StrToDouble(tbxDBL[i].Text.Trim()) + zh.StrToDouble(tbxCBL[i].Text.Trim()));//解决提交页面刷新后变动回旧值得问题
                    }
                    else//新增
                    {
                        if ((tbxDBL[i].Text.Trim() != "") || (tbxCBL[i].Text.Trim() != ""))
                        {
                            a01201_2 p = new a01201_2();

                            p.POLLUTE_OUTCOUNT = zh.StrToDouble(tbxDBL[i].Text.Trim()) + zh.StrToDouble(tbxCBL[i].Text.Trim());
                            p.REACH_COUNT = zh.StrToDouble(tbxDBL[i].Text.Trim());
                            p.OVER_COUNT = zh.StrToDouble(tbxCBL[i].Text.Trim());
                            p.SRKXH = Convert.ToString(i + 1);
                            p.CODE = dpl[i - 11].SelectedValue;
                            p.DATA_MONTH = Convert.ToString(Session["ShenBaoNianDu"]);
                            p.qiyeid = Convert.ToInt32(Session["qiyeid"]);
                            p.SERIAL = Convert.ToInt32(SERIAL);
                            mydata.a01201_2.InsertOnSubmit(p);
                            mydata.SubmitChanges();

                            hb[i].Value = "edit";
                            tbxHJ[i].Text = Convert.ToString(zh.StrToDouble(tbxDBL[i].Text.Trim()) + zh.StrToDouble(tbxCBL[i].Text.Trim()));//解决提交页面刷新后变动回旧值得问题
                        }
                    }
                }

                LabelTiShi.Visible = false;
                //Response.Write("<script>alert('你所填写申报内容正确，已成功提交!')</script>");
                MessageBox("tishi", "你所填写申报内容正确，已成功提交!");
            }
        }
    }

    //// <summary> 
    /// 弹出提示信息框 
    /// </summary> 
    /// <param name="strKey">函数key值</param> 
    /// <param name="strInfo">提示信息</param> 
    public void MessageBox(string strKey, string strInfo)
    {
        if (!ClientScript.IsClientScriptBlockRegistered(strKey))
        {
            string strjs = "alert('" + strInfo + "');";
            ClientScript.RegisterClientScriptBlock(this.GetType(), strKey, strjs, true);
        }
    } 

    protected void DropDownListPFK_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (DropDownListPFK.SelectedIndex > 0)
        {
            DuShuJu(DropDownListPFK.SelectedValue);
            HiddenFieldMain.Value = "edit";

            Session["PaiFangKou2-1"] = DropDownListPFK.SelectedValue;//传参数给打印页面用
        }
        else//新增排放口
        {
            ClearInput();
            Session["PaiFangKou2-1"] = "";//传参数给打印页面用
        }

    }
    protected void LinkButtonDelPFK_Click(object sender, EventArgs e)
    {
        if (DropDownListPFK.SelectedIndex == 0)
        {
            Response.Write("<script>alert('没有选择到要删除的排放口!')</script>");
            return;
        }

        using (DataClassesDataContext mydata = new DataClassesDataContext())
        {




            //在F_OUTPUT判断是否属于不能删除的排放口信息，
            // 如果suoding=1，则排放口及对应的本年度、以前所有年度申报数据都不能删除
            bool cz1 = (from f in mydata.F_OUTPUT
                        where f.qiyeid == Convert.ToInt32(Session["qiyeid"])
                        where f.suoding == true
                        where f.SERIAL == Convert.ToInt32(DropDownListPFK.SelectedValue)
                        select f).Any();

            if (cz1)
            {
                Response.Write("<script>alert('该排放口的申报信息已通过审核被锁定了，不能删除!')</script>");
                return;
            }

            //删除本年度已填报的申报数据,包括表a01201_1和表a01201_2。往年申报数据不能删除
            var rd1 = (from a in mydata.a01201_1
                       where a.DATA_MONTH == Convert.ToString(Session["ShenBaoNianDu"])
                       where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                       where a.SERIAL == Convert.ToInt32(DropDownListPFK.SelectedValue)
                       select a).FirstOrDefault();
            if (rd1 != null)
            {
                mydata.a01201_1.DeleteOnSubmit(rd1);
                mydata.SubmitChanges();
            }

            var rd2 = (from a in mydata.a01201_2
                       where a.DATA_MONTH == Convert.ToString(Session["ShenBaoNianDu"])
                       where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                       where a.SERIAL == Convert.ToInt32(DropDownListPFK.SelectedValue)
                       select a).FirstOrDefault();
            if (rd2 != null)
            {
                mydata.a01201_2.DeleteOnSubmit(rd2);
                mydata.SubmitChanges();
            }

            //只有当F_OUTPUT里的字段system=0和该排放口在本年度之前的年度没有申报数据两个条件同时成立，才能删除该排放口
            var cz2 = (from f in mydata.F_OUTPUT
                       where (f.system == null || f.system == false)
                       where f.qiyeid == Convert.ToInt32(Session["qiyeid"])
                       where f.SERIAL == Convert.ToInt32(DropDownListPFK.SelectedValue)
                       select f).Any();
            if (cz2)
            {
                bool cz3 = (from a in mydata.a01201_1
                            where a.DATA_MONTH.CompareTo(Convert.ToString(Session["ShenBaoNianDu"])) == -1
                            where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                            where a.SERIAL == Convert.ToInt32(DropDownListPFK.SelectedValue)
                            select a).Any();
                if (!cz3)
                {
                    bool cz4 = (from a in mydata.a01201_2
                                where a.DATA_MONTH.CompareTo(Convert.ToString(Session["ShenBaoNianDu"])) == -1
                                where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                                where a.SERIAL == Convert.ToInt32(DropDownListPFK.SelectedValue)
                                select a).Any();
                    if (!cz3)
                    {
                        var rd3 = (from f in mydata.F_OUTPUT
                                   where f.qiyeid == Convert.ToInt32(Session["qiyeid"])
                                   where f.SERIAL == Convert.ToInt32(DropDownListPFK.SelectedValue)
                                   select f).FirstOrDefault();
                        mydata.F_OUTPUT.DeleteOnSubmit(rd3);
                        mydata.SubmitChanges();

                        ClearInput();
                        ShuaXinXianShi("0");
                        Response.Write("<script>alert('删除排放口成功')</script>");
                    }
                }
            }
            else
            {
                Response.Write("<script>alert('该排放口被系统管理员锁定，不能删除!只能删除本年度的申报信息。')</script>");
                return;
            }
        }
    }

}